<template>
  <div style="height:100%;width:100%;">
    <div id="barLineCanvas" style="height:100%;width:100%;" ref="barLineCanvas"></div>
  </div>
</template>

<script>
import numeral from "numeral";
export default {
  name: "barLineCanvas",
  data() {
    return {};
  },
  props: {
    barData: {
      type: Array,
    },
    id: {
      type: String,
    },
  },
  watch: {
    barData(val) {
      this.init(val);
    },
  },
  mounted() {
    // this.init();
  },
  methods: {
    init(val) {
      var xdata = [];
      var obj = {};
      switch (this.id) {
        case "tab3-11":
          xdata = val.map((i) => i.jsdwF);

          obj = {
            预算数: {
              type: "bar",
              // data: val.map((i) => i.yusuan),
              data: val.map((i) => numeral(i.yusuan).format("0")),
            },
            本年资本性支出: {
              type: "bar",
              // data: val.map((i) => i.projectNum),
              data: val.map((i) => numeral(i.projectNum).format("0")),
            },
            投资完成率: {
              type: "line",
              data: val.map((i) => (i.jzPer && i.yusuanPer) || 0),
            },
          };
          break;
        case "tab3-12":
          xdata = val.map((i) => i.jsdwF);
          obj = {
            预算数: {
              type: "bar",
              // data: val.map((i) => i.yusuan),
              data: val.map((i) => numeral(i.yusuan).format("0")),
            },
            项目结转额: {
              type: "bar",
              // data: val.map((i) => i.proJz),
              data: val.map((i) => numeral(i.proJz).format("0")),
            },
            项目结转率: {
              type: "line",
              data: val.map((i) => (i.jzPer && i.jzPer) || 0),
            },
          };
          break;
        case "tab3-21":
          xdata = val.map((i) => i.yjzymcI);
          obj = {
            预算数: {
              type: "bar",
              // data: val.map((i) => i.yusuan),
              data: val.map((i) => numeral(i.yusuan).format("0")),
            },
            本年资本性支出: {
              type: "bar",
              // data: val.map((i) => i.projectNum),
              data: val.map((i) => numeral(i.projectNum).format("0")),
            },
            投资完成率: {
              type: "line",
              // data: val.map((i) =>  0),
              data: val.map((i) => i.yusuanPer)
            },
          };
          break;
        case "tab3-22":
          xdata = val.map((i) => i.yjzymcI);
          obj = {
            预算数: {
              type: "bar",
              // data: val.map((i) => i.yusuan),
              data: val.map((i) => numeral(i.yusuan).format("0")),
            },
            项目结转额: {
              type: "bar",
              // data: val.map((i) => i.proJz),
              data: val.map((i) => numeral(i.proJz).format("0")),
            },
            项目结转率: {
              type: "line",
              // data: val.map((i) => 0),
               data: val.map((i) => i.jzPer)
            },
          };
          break;
        default:
          break;
      }

      let legendata = Object.keys(obj);

      var myChart = this.$echarts.init(this.$refs.barLineCanvas);
      let option = {
        color: [
          //自定义的颜色
          new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "#49DFFE" },
            { offset: 1, color: "#41A2FD" }, //柱图渐变色
          ]),
          new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "#7EDE81" },
            { offset: 1, color: "#71CEAA" }, //柱图渐变色
          ]),
          "#F3707C",
        ],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#F1F1F1",
            },
          },
        },
        grid: {
          left: "1%",
          right: "1%",
          bottom: "10%",
          top: "13%",
          containLabel: true,
        },
        legend: {
          data: legendata,
          right: "3%",
          top: "4%",
          // itemWidth: 12,
          itemHeight: 12,
          itemGap: 20,
          textStyle: {
            fontSize: 12,
          },
        },
        xAxis: [
          {
            type: "category",
            data: xdata,
            axisPointer: {
              type: "shadow",
            },
            axisLabel: {
              show: true,
              rotate: 45,
              // textStyle: {
              //   color: "#000",
              //   fontSize: 12,
              // },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            // axisLine: {
            //   lineStyle: {
            //     color: "#eee",
            //     width: 1, // 粗细
            //   },
            // },
          },
        ],
        yAxis: [
          {
            // type: this.id == 'tab3-11' || this.id == 'tab3-12' ? "log" : 'value',
            type: 'value',
            name: "单位：万元",
            nameTextStyle: {
              fontSize: 14,
              padding: [0, 0, 5, 50],
              color: "rgba(0, 0, 0, 0.4)",
            },
            axisLine: {
              lineStyle: {
                color: "#eee",
                width: 1, // 粗细
              },
            },

            axisLabel: {
              show: true,
              textStyle: {
                color: "#000",
                fontSize: 12,
              },
            },
            axisTick: {
              show: false,
            },
          },
          {
            type: "value",
            splitLine: {
              //网格线
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "#eee",
                width: 1, // 粗细
              },
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#000",
                fontSize: 12,
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: legendata[0],
            type: "bar",
            label: {
              show: true,
              position: "top",
              rotate: 45,
            },
            barMaxWidth: "18px",
            // barGap: 0,
            data: obj[legendata[0]].data,
          },
          {
            name: legendata[1],
            type: "bar",
            // barGap: 0,
            label: {
              show: false,
              position: "top",
              rotate: 45,
            },
            barMaxWidth: "18px",
            data: obj[legendata[1]].data,
          },
          {
            name: legendata[2],
            type: "line",
            yAxisIndex: 1,
            // smooth: this.id == "tab3-11" || this.id == "tab3-21" ? false : true,
            smooth:  false ,
            lineStyle: {
              color: "#F3707C",
              width: 3,
            },
            data: obj[legendata[2]].data,
          },
        ],
        // dataZoom: [
        //     {
        //         show: true,
        //         yAxisIndex: 0,
        //         filterMode: 'empty',
        //         width: 20,
        //         height: '40%',
        //         showDataShadow: false,
        //         left: '90%'
        //     }
        // ],
      };
      myChart.setOption(option);
      setTimeout(function () {
        window.addEventListener("resize", () => {
          myChart.resize();
        });
      }, 500);
    },
  },
};
</script>

<style>
</style>
